package com.jh.readme.iu.fragment.newslist;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.apkfuns.logutils.LogUtils;
import com.jh.readme.R;
import com.jh.readme.iu.fragment.newslist.ViewHolder.NewsListVH;
import com.jh.tools.base.BaseFragment;
import com.jh.tools.bean.NeteastNewsSummary;
import com.jh.tools.view.TRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;

/**
 * A fragment representing a list of Items.
 * <p/>
 * interface.
 * 新闻列表
 */
public class NewsListFragment extends BaseFragment<NewsListPresenter,NewsListModel>implements NewsListContract.View {
    public static final String NEWS_ID = "news_id";
    public static final String NEWS_TYPE = "news_type";
    public static final String POSITION = "position";
    private static final String news_title="title_news";
    private RecyclerView news_content;
    private MyRecyclerAdapter mCommAdapter ;
    TRecyclerView mTRecyclerView;
    public static NewsListFragment newInstance(String newsId, String newsType, int position) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NEWS_ID, newsId);
        bundle.putString(NEWS_TYPE, newsType);
        bundle.putInt(POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }
    public static NewsListFragment newInstance(String title,String newsId,String newsType, int position) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NEWS_ID, newsId);
        bundle.putString(NEWS_TYPE, newsType);
        bundle.putString(news_title,title);
        bundle.putInt(POSITION, position);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public int getLayoutId() {
        return R.layout.fragment_item_list;
    }

    @Override
    public void initView(View baseView) {
        final Bundle bundle = this.getArguments();
        mTRecyclerView=(TRecyclerView)baseView.findViewById(R.id.trv_context);
        mTRecyclerView.setView(NewsListVH.class,mPresenter)
                .setParam(NEWS_TYPE,bundle.getString(NEWS_TYPE))
                .setParam(NEWS_ID,bundle.getString(NEWS_ID))
                .setIsRefreshable(true)
                .fetch();

//        mTRecyclerView.model.getPageAt(1).subscribe(new Observer<Map<String, List<NeteastNewsSummary>>>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(Map<String, List<NeteastNewsSummary>> stringListMap) {
//                mTRecyclerView.getAdapter().setBeans(stringListMap.get(bundle.getString(NEWS_TYPE)),1);
//            }
//        });
        news_content=(RecyclerView) baseView.findViewById(R.id.news_content);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置布局管理器
        news_content.setLayoutManager(layoutManager);
//        //设置Adapter
//        news_content.setAdapter( recycleAdapter);
        //设置增加或删除条目的动画
        news_content.setItemAnimator(new DefaultItemAnimator());
        Map<String,String> maps=new HashMap<>();
        maps.put(NEWS_TYPE,bundle.getString(NEWS_TYPE));
        maps.put(NEWS_ID,bundle.getString(NEWS_ID));
//        mPresenter.getInitView(maps);
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel,this);
        mPresenter.onStart();
    }

    @Override
    public void initNewList(List<NeteastNewsSummary> neteastNewsSummary) {
        if (null==mCommAdapter){
            mCommAdapter = new MyRecyclerAdapter(this.getActivity(),neteastNewsSummary);
            news_content.setAdapter(mCommAdapter);
        }


    }
}
